package com.itmk.web.select_seat.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itmk.web.back_out.entity.BackOut;
import com.itmk.web.back_out.service.BackOutService;
import com.itmk.web.select_seat.entity.SelectSeat;
import com.itmk.web.select_seat.entity.SelectSeatParm;
import com.itmk.web.select_seat.entity.SignParm;
import com.itmk.web.select_seat.mapper.SelectSeatMapper;
import com.itmk.web.select_seat.service.SelectSeatService;
import com.itmk.web.wxapi.entity.EchartsItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author java实战基地
 * @since 2023-10-28
 */
@Service
public class SelectSeatServiceImpl extends ServiceImpl<SelectSeatMapper, SelectSeat> implements SelectSeatService {
    @Autowired
    private BackOutService backOutService;

    @Override
    public IPage<SelectSeat> getList(SelectSeatParm parm) {
        IPage<SelectSeat> page = new Page<>(parm.getCurrentPage(),parm.getPageSize());
        return this.baseMapper.getList(page,parm);
    }

    @Override
    @Transactional
    public void signSeat(SignParm signParm) {
        //查询预约信息
        SelectSeat seat = this.baseMapper.selectById(signParm.getSelectId());
        //更新预约表状态
        SelectSeat selectSeat = new SelectSeat();
        selectSeat.setSelectId(signParm.getSelectId());
        selectSeat.setStatus("2");
        this.baseMapper.updateById(selectSeat);
        //插入签到明显
        BackOut backOut = new BackOut();
        backOut.setStatus(signParm.getStatus());
        backOut.setStuId(seat.getStuId());
        backOut.setSeatName(seat.getSeatName());
        backOut.setFloorName(seat.getFloorName());
        backOut.setRoomName(seat.getRoomName());
        backOut.setOrderTime(seat.getOrderTime());
        backOut.setLastTime(seat.getLastTime()); //最迟签到时间
        backOut.setCreateTime(LocalDateTime.now());
        backOutService.save(backOut);
    }

    @Override
    @Transactional
    public void signOut(SignParm signParm) {
        //查询预约信息
        SelectSeat seat = this.baseMapper.selectById(signParm.getSelectId());
        //更新预约表状态
        SelectSeat selectSeat = new SelectSeat();
        selectSeat.setSelectId(signParm.getSelectId());
        selectSeat.setStatus("3");
        selectSeat.setUseStatus("0"); //签退后座位设为未使用
        this.baseMapper.updateById(selectSeat);
        //插入签到明显
        BackOut backOut = new BackOut();
        backOut.setStatus(signParm.getStatus());
        backOut.setStuId(seat.getStuId());
        backOut.setSeatName(seat.getSeatName());
        backOut.setFloorName(seat.getFloorName());
        backOut.setRoomName(seat.getRoomName());
        backOut.setOrderTime(seat.getOrderTime());
        backOut.setLastTime(signParm.getLastTime());//关门时间：最迟签退时间
        backOut.setCreateTime(LocalDateTime.now());
        backOutService.save(backOut);
    }

    @Override
    public List<EchartsItem> getTotalMonth() {
        return this.baseMapper.getTotalMonth();
    }

    @Override
    public List<EchartsItem> getHotRoom() {
        return this.baseMapper.getHotRoom();
    }
}
